home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / c / AMPlifier_SDK.lha / amplifier_sdk / SampleVis.c < prev   
C/C++ Source or Header  |  1999-01-27  |  6KB  |  308 lines

  1. /*
  2. ** AMPlifier - example visualization plugin
  3. **
  4. ** $VER: SampleVis.c  1999 Thorsten Hansen
  5. */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <dos/dos.h>
  10. #include <graphics/gfxbase.h>
  11. #include <wbstartup.h>
  12.  
  13. #include <clib/exec_protos.h>
  14. #include <clib/alib_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #include <clib/utility_protos.h>
  17. #include <clib/graphics_protos.h>
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include <libraries/amplifierplugin.h>
  24. #include <clib/amplifierplugin_protos.h>
  25. #include <pragma/amplifierplugin_lib.h>            // Maxon, StormC
  26. //#include <pragmas/amplifierplugin_pragmas.h>    // SAS,...
  27.  
  28.  
  29. #define VIS_WIDTH        256
  30. #define VIS_HEIGHT    128
  31.  
  32.  
  33. struct Library *UtilityBase = NULL;
  34. struct Library *IntuitionBase = NULL;
  35. struct Library *GfxBase = NULL;
  36.  
  37. struct Library *AMPlifierPluginBase = NULL;
  38.  
  39. BOOL OpenLibs()
  40. {
  41.     UtilityBase = OpenLibrary("utility.library", 39);
  42.     IntuitionBase = OpenLibrary("intuition.library", 39);
  43.     GfxBase = OpenLibrary("graphics.library", 39);
  44.  
  45.     return (ULONG) (UtilityBase && IntuitionBase && GfxBase);
  46. }
  47.  
  48. void CloseLibs()
  49. {
  50.     if (UtilityBase)
  51.         CloseLibrary(UtilityBase);
  52.     if (IntuitionBase)
  53.         CloseLibrary(IntuitionBase);
  54.     if (GfxBase)
  55.         CloseLibrary(GfxBase);
  56. }
  57.  
  58. //*************************************
  59.  
  60. struct Hook renderhook = { NULL, NULL, NULL, NULL };
  61. struct Window *win = NULL;
  62. UBYTE buffer[VIS_WIDTH*VIS_HEIGHT];
  63. UWORD max[2][128];
  64. UBYTE color[64];
  65.  
  66.  
  67. struct PluginCtrl * PI_AddModule(Tag tag1,...)
  68. {
  69.     return PI_AddModuleA((struct TagItem *) &tag1);
  70. }
  71.  
  72. void Req(char *text, char *gadget)
  73. {
  74.     struct EasyStruct es =
  75.     { sizeof(struct EasyStruct), 0 , "VisPlugin", text , gadget };
  76.     EasyRequestArgs(NULL, &es, NULL, NULL);
  77. }
  78.  
  79.  
  80. BOOL PluginStart()
  81. {
  82.     BOOL result = FALSE;
  83.     int i;
  84.  
  85.     win = OpenWindowTags(NULL,
  86.         WA_Left, 50,
  87.         WA_Top, 50,
  88.         WA_InnerWidth, VIS_WIDTH,
  89.         WA_InnerHeight, VIS_HEIGHT,
  90.         WA_Title, "AMPlifier Sample Visualization",
  91.         WA_ScreenTitle, "AMPlifier Sample Visualization  written by Thorsten Hansen",
  92.         WA_DragBar, TRUE,
  93.         WA_DepthGadget, TRUE,
  94.         WA_CloseGadget, TRUE,
  95.         WA_IDCMP, CLOSEWINDOW,
  96.         TAG_END);
  97.     if (win)
  98.     {
  99.         for (i = 0; i < 64; i++)
  100.         {
  101.             color[i] = ObtainBestPen(win->WScreen->ViewPort.ColorMap,
  102.                 0xdd000000,
  103.                 i * 0x4000000,
  104.                 0x00000000,
  105.                 TAG_END);
  106.         }
  107.  
  108.         result = TRUE;
  109.     }
  110.  
  111.     return result;
  112. }
  113.  
  114. void PluginEnd()
  115. {
  116.     int i;
  117.  
  118.     if (win)
  119.     {
  120.         for (i = 0; i < 64; i++)
  121.             ReleasePen(win->WScreen->ViewPort.ColorMap, color[i]);
  122.  
  123.         CloseWindow(win);
  124.         win = NULL;
  125.     }
  126. }
  127.  
  128. /*
  129.  * RenderFunc - called from AMPlifier with new pcm/spectrum data
  130.  *
  131.  * struct RenderData have the following data:
  132.  *   Channels    - number of channels
  133.  *   Waveform[2] - Arrays with pcm data (512 entrys with signed WORDs)
  134.  *   Spectrum[2] - Arrays with spectrum data (256 entrys with a range 0-255)
  135.  */
  136. long RenderFunc(register __a0 struct Hook *hook, register __a2 struct PluginCtrl *pctrl, register __a1 struct RenderData *rd)
  137. {
  138.     struct RastPort *rp = win->RPort;
  139.     int ch, x, y, yoffs, yoffs2, xoffs, c;
  140.     int left = win->BorderLeft;
  141.     int top = win->BorderTop;
  142.  
  143.     // clear buffer
  144.     memset(buffer, 1, VIS_WIDTH*VIS_HEIGHT);
  145.  
  146.     // render oscilloscope
  147.     yoffs = VIS_HEIGHT/4;
  148.     for (ch = 0; ch < rd->Channels; ch++)
  149.     {
  150.         for (x = 0; x < 256; x++)
  151.         {
  152.             y = ((rd->waveform[ch][x<<1]>>10) + yoffs) << 8;
  153.             buffer[x+y] = 2;
  154.         }
  155.         yoffs += VIS_HEIGHT/2;
  156.     }
  157.  
  158.     // render spectrum analyzer
  159.     xoffs = 0;
  160.     for (ch = 0; ch < rd->Channels; ch++)
  161.     {
  162.         for (x = 0; x < 64; x++)
  163.         {
  164.             UWORD s;
  165.             if (ch == 0)
  166.                 s = rd->spectrum[ch][(63 - x)<<2] >> 2;
  167.             else
  168.                 s = rd->spectrum[ch][x<<2] >> 2;
  169.  
  170.             if (s >= max[ch][x])
  171.                 max[ch][x] = s;
  172.             else if (max[ch][x] > 4)
  173.                 max[ch][x] -= 4;
  174.             else
  175.                 max[ch][x] = 0;
  176.  
  177.             y = 64 - max[ch][x];
  178.             yoffs = xoffs + (x<<1) + (y<<8);
  179.             yoffs2 = xoffs + (x<<1) + ((127-y)<<8);
  180.             c = 0;
  181.             for (; y < 64; y++)
  182.             {
  183.                 buffer[yoffs] = color[c];
  184. //                buffer[yoffs+1] = color[c];
  185. //                buffer[yoffs+2] = color[c];
  186.                 yoffs += VIS_WIDTH;
  187.                 buffer[yoffs2] = color[c];
  188. //                buffer[yoffs2+1] = color[c];
  189. //                buffer[yoffs2+2] = color[c];
  190.                 yoffs2 -= VIS_WIDTH;
  191.                 c++;
  192.             }
  193.         }
  194.         xoffs += VIS_WIDTH/2;
  195.     }
  196.  
  197.     WriteChunkyPixels(rp, left, top, left + VIS_WIDTH - 1, top + VIS_HEIGHT - 1, buffer, VIS_WIDTH);
  198.  
  199.     return 0;
  200. }
  201.  
  202.  
  203. BOOL HandleIEvents(struct Window *win)
  204. {
  205.     BOOL retval = FALSE;
  206.    struct IntuiMessage *imsg;
  207.  
  208.    while (imsg = (struct IntuiMessage *) GetMsg(win->UserPort))
  209.     {
  210.         switch(imsg->Class)
  211.         {
  212.             case IDCMP_CLOSEWINDOW:
  213.             {
  214.                 retval = TRUE;;
  215.                 break;
  216.             }
  217.         }
  218.  
  219.         ReplyMsg((Message *) imsg);
  220.     }
  221.     return retval;
  222. }
  223.  
  224. void Run()
  225. {
  226.     BOOL quit = FALSE;
  227.     ULONG gotsigs;
  228.     ULONG sigmask = SIGBREAKF_CTRL_C;
  229.     ULONG winsig = (1L << win->UserPort->mp_SigBit);
  230.     sigmask |= winsig;
  231.  
  232.     while (!quit)
  233.     {
  234.         gotsigs = Wait(sigmask);
  235.  
  236.         if (gotsigs & SIGBREAKF_CTRL_C)
  237.         {
  238.             quit = TRUE;
  239.         }
  240.         if (gotsigs & winsig)
  241.         {
  242.             if (HandleIEvents(win))
  243.                 quit = TRUE;
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249. void main(int argc, char *argv[])
  250. {
  251.     if (OpenLibs())
  252.     {
  253.         // This demo requires Amiga OS 3.1 (WriteChunkyPixels)
  254.         if (GfxBase->lib_Version < 40)
  255.         {
  256.             Req("This plugin requires at least Amiga OS 3.1.", "Ok");
  257.             CloseLibs();
  258.             return;
  259.         }
  260.  
  261.         // Try to open AMPlifiers plugin library
  262.         AMPlifierPluginBase = OpenLibrary("amplifierplugin.library", 0);
  263.         if (AMPlifierPluginBase)
  264.         {
  265.             // Setup plugin
  266.             if (PluginStart())
  267.             {
  268.                 struct PluginCtrl *pctrl;
  269.                 renderhook.h_Entry = (ULONG (*)()) &RenderFunc;
  270.  
  271.                 // Connect plugin to AMPlifiers pluginlist
  272.                 // When AMPlifier quits it sends a signal to the given task.
  273.                 // The plugin must quit after receiving this signal.
  274.                 pctrl = PI_AddModule(
  275.                     PIA_Name, "SampleVis",
  276.                     PIA_QuitTask, FindTask(NULL),
  277.                     PIA_QuitMask, SIGBREAKF_CTRL_C,
  278.                     PIA_RenderHook, &renderhook,
  279.                     PIA_WaveformChannels, 2,
  280.                     PIA_SpectrumChannels, 2,
  281.                     TAG_END);
  282.                 if (pctrl)
  283.                 {
  284.                     // Event loop
  285.                     Run();
  286.  
  287.                     // Remove plugin module from AMPlifier
  288.                     PI_RemModule(pctrl);
  289.                 }
  290.                 else
  291.                     Req("Add module failed.", "hmm...");
  292.  
  293.                 // Free plugin resources
  294.                 PluginEnd();
  295.             }
  296.  
  297.             // Finaly close AMPlifiers plugin library.
  298.             // AMPlifier will not be able to quit until the library is closed.
  299.             CloseLibrary(AMPlifierPluginBase);
  300.         }
  301.         else
  302.             Req("Couldn't open AMPlifier plugin library!\nAMPlifier probably not running.", "Ok");
  303.  
  304.         CloseLibs();
  305.     }
  306. }
  307.  
  308.